//
// Created by benxb on 2021/11/16.
//

#ifndef BENXDB_DATA_CHUNK_STORAGE_H
#define BENXDB_DATA_CHUNK_STORAGE_H

#include <string>
#include <fstream>

namespace benxdb {

class DataChunkStorage {
public:
    /**
     * Create a new column storage that writes to the specified database file
     * @param db_file the file name of the database file
     */
    explicit DataChunkStorage(const std::string &table_name);

    ~DataChunkStorage() = default;

    /**
     * Shut down the disk manager and close all the file resources
     */
    void ShutDown();

    /**
     * Write data_ of page into database file
     * @param chunk_data
     * @param chunk_size
     */
    void AppendChunkData(const char *chunk_data, int64_t chunk_size);

    /**
     * Read page data_ from a database file
     * @param chunk_offset
     * @param chunk_size
     * @param chunk_data
     */
    void ReadChunkData(int64_t chunk_offset, int64_t chunk_size, std::string& chunk_data);

    /**
     * Read page data_ from a database file. This function for fixed length chunk and no compress
     * @param chunk_offset
     * @param chunk_size
     * @param chunk_data
     */
    void ReadChunkData(int64_t chunk_offset, int64_t chunk_size, char* chunk_data);

    /**
     * Get next free data_ chunk offset. In fact is data_ file size.
     * @return chunk offset
     */
    int64_t GetNextDataChunkOffset();

    std::string GetDataFile() {
        return data_file_;
    }

private:
    // stream to write db file
    std::fstream db_io_;
    std::string data_file_;
    int64_t file_size_;
    int num_flushes_{};
    int num_writes_{};
};

}


#endif //BENXDB_DATA_CHUNK_STORAGE_H
